<!--
 * @Description: 
 * @Author: mmzhang
 * @Date: 2022-11-14 10:12:04
 * @LastEditTime: 2022-12-14 15:48:44
-->
<template>
  <div style="padding: 10px">
    <dt-search ref="search" :label-width="90" :xlBtn='6'>
      <Button type="primary" v-if="unlocking('0207010301')" style="margin-left: 8px"
        @click="exportExcel">导出</Button>
    </dt-search>
    <dt-grid ref="table" :page='page' :onFetch='onFetch' />
    <enclosure-model ref="enclosure" :title="enclosure && enclosure.name"
      @downLoadAll='onDownloadEnclosure'></enclosure-model>
  </div>
</template>

<script>
  import Column from './components/achievementConfirmColumn'
  import achievementConfirmMgr from "@/biz/achievementConfirm";
  import toolApi from "@/biz/base/tool";
  import SearchOpt from './components/achievementConfirmSearch'
  import enclosureModel from '@/components/enclosureModel.vue'
  import buildMgr from '@/biz/building'
  import store from '@/store'
  import caseMgr from "@/biz/crimeSceneList";
  import { buildPage } from '@/router/buildPage.js'
  import moment from 'moment'
  export default {
    components: {
      enclosureModel
    },
    data() {
      return {
        loading: false,
        option: {
          orgIds: [],
          cityCode: "",
          housesName: "",
          status: "",
        },
        searchInfo:{
          companyIds:[],
          buildingId:null,
          year:'',
          quarter:''
        },
        page: {
          total: 0,
        },
        enclosure: null
      }
    },
    created() {},
    mounted() {
      this.$refs.table.init(this, new Column())
      this.$refs.search.init(this.$refs.table, new SearchOpt(this),this
        .searchInfo)
      this.$refs.table.fetch()
    },
    methods: {
      onFetch(data) {
        if(data.quarter && data.quarter!='' && (data.year==null || data.year=='')){
          this.option = data
          this.$Message.info('年和季度必须同时选择')
          return new Promise((resolve, reject) => {
            resolve()
          }).then(res => {
            return {
              list: [],
              total: 0
            }
          })
        }else{
          if(data.year && data.year!=''){
            let quarterArr = data.quarter.split('-')
            data.startTime =  moment(data.year+'-'+quarterArr[0]).startOf("month").format("YYYY-MM-DD")
            data.endTime =  moment(data.year+'-'+quarterArr[1]).endOf("month").format("YYYY-MM-DD")
          }
          this.option = data
          return achievementConfirmMgr.list(this.option).then(res => {
            this.page.total = res.total
            return res.list
          })
        }
      },
      exportExcel() {
        if(this.option.quarter && this.option.quarter!='' && (this.option.year==null || this.option.year=='')){
          this.$Message.info('年和季度必须同时选择')
          return false
        }
        achievementConfirmMgr.export(this.option)
      },
      onFile(opt) {
        this.enclosure = opt
        achievementConfirmMgr.enclosureSearch(this.enclosure.value).then(res => {
          this.$refs.enclosure.show({
            images: res.imgList.map(itm => {
              return {
                url: itm.url,
                name: itm.name
              }
            }),
            files: res.fileList.map(itm => {
              return {
                url: itm.url,
                name: itm.name
              }
            }),
            canDownLoad: this.unlocking('0207010302')
          })
        })
      },
      onDownloadEnclosure() {
        achievementConfirmMgr.enclosureDownload(this.enclosure.value, this.enclosure.type)
      },
      linkDetailFun(housesId){
        let params = {
          housesId: housesId
        }
        caseMgr.buildinghousesList(params).then((res) => {
          let item = { ...res.list[0] }
          // 更新案场
          buildMgr.updateBuildInfo(item)
          this.$tool.setStorage({ caseBuildingId: JSON.stringify(item.housesId) })
          store.dispatch('setRouterList', buildPage)

          this.$router.push({
            name: 'ownerPerformanceProve'
          }) // 甲方业绩确认证明
        })
      }
    },
  };
</script>

<style lang="scss" scoped>
  .channel-audit {
    padding: 20px;
  }

  .searchBox {
    padding: 20px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
  }
</style>